package com.dagene.weixin.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JSONObject;

import com.dagene.weixin.bean.AccessToken;
import com.dagene.weixin.bean.msg.NoticeMsg;
import com.dagene.weixin.bean.msg.TemplateMsg;
import com.dagene.weixin.bean.msg.ValueColor;
import com.dagene.weixin.util.Constants;
import com.dagene.weixin.util.HttpUtil;

public class TemplateMsgService {
	String touser;
	String title;
	String goodsname;
	HttpUtil httpUtil = new HttpUtil();
	String httputilUrl;
	// 意见反馈通知
	String[] templateIds = { "v20vMCxl3KeYsVLsOTfmKD8lhvkAagf6lyfVFobjaiY" };

	public TemplateMsgService(String touser, String goodsname, String title) {
		this.touser = touser;
		this.goodsname = goodsname;
		this.title = title;
		AccessToken at = httpUtil.getAccessToken(Constants.APPID,
				Constants.APPSECRET);
		httputilUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
		// 每次发送7条
		httputilUrl = httputilUrl.replace("ACCESS_TOKEN", at.getToken());
	}

	public void sendMsg() {

		TemplateMsg msg = new TemplateMsg();
		msg.setTouser(touser);
		msg.setTopcolor("#FF0000");
		// if (order.getOrderstatus()) {
		NoticeMsg nmsg = new NoticeMsg();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		// 客服聊天通知
		msg.setTemplate_id(templateIds[0]);
		nmsg.setFirst(new ValueColor(title, "#173177"));
		nmsg.setKeyword1(new ValueColor(goodsname, "#173177"));
		nmsg.setKeyword2(new ValueColor("客服聊天匹配", "#173177"));
		nmsg.setKeyword3(new ValueColor(sdf.format(new Date()), "#173177"));
		msg.setData(nmsg);

		JSONObject json = JSONObject.fromObject(msg);
		httpUtil.httpPostRequest(httputilUrl, "", json.toString());
		/*
		 * "touser":"OPENID",
		 * "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
		 * "url":"http://weixin.qq.com/download", "topcolor":"#FF0000", "data":{
		 * "User": { "value":"黄先生", "color":"#173177" }, }
		 */
	}
}
